package com.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.common.Result;
import com.example.entity.HnyyFeedback;
import com.example.service.FeedbackService;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.OffsetDateTime;

@RestController
@CrossOrigin
@RequestMapping("/feedback")
public class FeedBackController {
    @Autowired
    private FeedbackService feedbackService;
    @PostMapping("/add")
    public Result add(@RequestBody HnyyFeedback hnyyFeedBack) throws Exception {
        System.out.println("hnyyFeedBack======>" + JSONObject.toJSONString(hnyyFeedBack));
        OffsetDateTime odt = OffsetDateTime.parse(hnyyFeedBack.getCreateTimeStr());
        Timestamp timestamp = Timestamp.from(odt.toInstant());
        hnyyFeedBack.setCreateTime(timestamp);
        int num = feedbackService.getNum() + 1;
        hnyyFeedBack.setHnyyFeedbackId(String.valueOf(num));
        Integer addFeedback = feedbackService.addFeedback(hnyyFeedBack);
        if (addFeedback == 0) {
            return Result.error("添加失败");
        }
        return Result.success(addFeedback);
    }

    @GetMapping("/getfeedback")
    public Result getPickingCartonDetails(@RequestParam Integer pageNum,
                                          @RequestParam Integer pageSize,
                                          @RequestParam (required = false) String name,
                                          @RequestParam (required = false) String type,
                                          HttpServletRequest request)
    {
        PageInfo<HnyyFeedback> info = feedbackService.selectPickingCartonDetails(pageNum, pageSize, name,type);
        return Result.success( info);
    }
    @PostMapping("update")
    public Result update(@RequestBody HnyyFeedback hnyyFeedBack) throws Exception {
        System.out.println("hnyyFeedBack======>" + JSONObject.toJSONString(hnyyFeedBack));
        int update = feedbackService.update(hnyyFeedBack);
        if (update == 0) {
            return Result.error("修改失败");
        }
        return Result.success(update);
    }
}
